#include "usart.h"

#include "status.h"
/*---------------------------- UART3 9600 颜色传感器  ---------------------------*/

UART_HandleTypeDef uart3_handle;

u8 uart3_rx_data;
u8 color_sensor_data[5];

void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef GPIO_Config;

  if (huart->Instance == UART4)
  {
    __HAL_RCC_GPIOC_CLK_ENABLE();      //使能GPIOC时钟
    __HAL_RCC_UART4_CLK_ENABLE();      //使能USART4时钟
    
    GPIO_Config.Pin = GPIO_PIN_10 | GPIO_PIN_11;
    GPIO_Config.Mode = GPIO_MODE_AF_PP;    //复用推挽输出
    GPIO_Config.Pull = GPIO_PULLUP;      //上拉
    GPIO_Config.Speed = GPIO_SPEED_FAST;    //高速
    GPIO_Config.Alternate = GPIO_AF8_UART4;  //复用为USART4
    HAL_GPIO_Init(GPIOC, &GPIO_Config);

  } 
  else if (huart->Instance == USART3)
  {
    __HAL_RCC_GPIOD_CLK_ENABLE();      //使能GPIOD时钟
    __HAL_RCC_USART3_CLK_ENABLE();      //使能USART3时钟

    GPIO_Config.Pin = GPIO_PIN_8 | GPIO_PIN_9;
    GPIO_Config.Mode = GPIO_MODE_AF_PP;    //复用推挽输出
    GPIO_Config.Pull = GPIO_PULLUP;      //上拉
    GPIO_Config.Speed = GPIO_SPEED_FAST;    //高速
    GPIO_Config.Alternate = GPIO_AF7_USART3;  //复用为USART3
    HAL_GPIO_Init(GPIOD, &GPIO_Config);

    HAL_NVIC_EnableIRQ(USART3_IRQn);    //使能USART3中断通道
    HAL_NVIC_SetPriority(USART3_IRQn, 3, 0);  //抢占优先级3，
  }
}

void uart3_init(void)
{
  uart3_handle.Instance          = USART3;
  
  uart3_handle.Init.BaudRate     = 9600;
  uart3_handle.Init.WordLength   = UART_WORDLENGTH_8B;
  uart3_handle.Init.StopBits     = UART_STOPBITS_1;
  uart3_handle.Init.Parity       = UART_PARITY_NONE;
  uart3_handle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  uart3_handle.Init.Mode         = UART_MODE_TX_RX;
  
  HAL_UART_Init(&uart3_handle);
  
  HAL_UART_Receive_IT(&uart3_handle, &uart3_rx_data, 1);//该函数会开启接收中断：标志位UART_IT_RXNE，并且设置接收缓冲以及接收缓冲接收最大数据量
    
  /*使能串口接收断 */
  __HAL_UART_ENABLE_IT(&uart3_handle,UART_IT_RXNE);  
}


void USART3_IRQHandler(void)
{
  if (__HAL_UART_GET_FLAG(&uart3_handle, UART_FLAG_RXNE) != RESET)
  {
    uart3_rx_data = (uint16_t) READ_REG(uart3_handle.Instance->DR);
    
    status.color_msg++;
    
//    WRITE_REG(uart3_handle.Instance->DR, uart3_rx_data);
  }
}


/*  发送字符串 */
void usart3_sendstring(uint8_t *str)
{
	unsigned int k=0;
  do
  {
      HAL_UART_Transmit(&uart3_handle,(uint8_t *)(str + k) ,1,1000);
      k++;
  } while(*(str + k)!='\0');
}

/*---------------------------- UART4 115200 调试输出  ---------------------------*/

UART_HandleTypeDef uart4_handle;

void uart4_init(void)
{
  uart4_handle.Instance          = UART4;
  
  uart4_handle.Init.BaudRate     = 115200;
  uart4_handle.Init.WordLength   = UART_WORDLENGTH_8B;
  uart4_handle.Init.StopBits     = UART_STOPBITS_1;
  uart4_handle.Init.Parity       = UART_PARITY_NONE;
  uart4_handle.Init.HwFlowCtl    = UART_HWCONTROL_NONE;
  uart4_handle.Init.Mode         = UART_MODE_TX_RX;
  
  HAL_UART_Init(&uart4_handle);
    
  __HAL_UART_ENABLE_IT(&uart4_handle,UART_IT_RXNE);   /*使能串口接收断 */
}

void uart4_sendstring(uint8_t *str)
{
	unsigned int k=0;
  do 
  {
      HAL_UART_Transmit(&uart4_handle,(uint8_t *)(str + k) ,1,1000);
      k++;
  } while(*(str + k)!='\0');
}


int fputc(int ch, FILE *f)
{
	HAL_UART_Transmit(&uart4_handle, (uint8_t *)&ch, 1, 1000);	
	return (ch);
}

///重定向c库函数scanf到串口DEBUG_USART，重写向后可使用scanf、getchar等函数
int fgetc(FILE *f)
{
	int ch;
	HAL_UART_Receive(&uart4_handle, (uint8_t *)&ch, 1, 1000);	
	return (ch);
}

void uart_init(void)
{
  uart3_init();
  uart4_init();
}



/*---------------------------------                   --------------------------------------*/

#pragma import(__use_no_semihosting)
//标准库需要的支持函数                 
struct __FILE
{
  int handle;
};

FILE __stdout;
 
void _sys_exit(int x)
{
  x = x;
}



